package com.haidechizi.leetcode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author: wangdejun
 * @create: 2020-04-02 11:16
 **/
public class _6_从尾到头打印链表 {

    public int[] reversePrint(ListNode head) {
        head = reverse(head);
        List<Integer> list = new ArrayList<>();
        int length = 0;
        while (head != null) {
            list.add(head.val);
            head = head.next;
            length++;
        }
        int[] result = new int[length];
        for (int i = 0; i < length;i++) {
            result[i] = list.get(i);
        }
        return result;
    }

    private ListNode reverse(ListNode node) {
        ListNode head = new ListNode(-1);
        while (node != null) {
            ListNode next = head.next;
            head.next = node;
            ListNode next1 = node.next;
            node.next = next;
            node = next1;
        }
        return head.next;
    }
}
